package com.javaweb.newservlelt;


import com.adminx.utils.ModelAndView;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * LoginServlet for login request.
 * there are two ways for configuring servlet
 *  A. in web.xml
 *  B. @WebServlet
 */

@WebServlet( "/test/newNativeAjax")
public class NativeAjaxServlet extends HttpServlet {
    private Logger logger = LogManager.getLogger(NativeAjaxServlet.class) ;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp) ;
    }
    /**
     * override doPost logic from father by our own business logic
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String nationId = req.getParameter("nationId") ;
        logger.debug("got parameter : {} " , nationId );
        String cityStr = null ;
        // processing data here.....
        switch (nationId) {
            case "1":
                cityStr = "北京|上海|广东|深圳|西安|杭州";
                break;
            case "2":
                cityStr = "NewYork|Washington|LosAngles|Chicago";
                break;
            case "3":
                cityStr = "东京|大阪|横滨|名古屋";
                break;
        }
        resp.setContentType("application/text; charset=utf-8");
        PrintWriter out = resp.getWriter() ;
        out.print(cityStr) ;
        out.flush() ;

    }



}





